package org.chatboleh.frame.internal;

import java.awt.Container;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JInternalFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;

import org.apache.log4j.Logger;
import org.chatboleh.ApplicationFrame;
import org.chatboleh.bean.Payee;
import org.chatboleh.exception.EDMException;
import org.chatboleh.table.PayeeListTable;
import org.chatboleh.util.MessageBox;
import org.dyno.visual.swing.layouts.Bilateral;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;

//VS4E -- DO NOT REMOVE THIS LINE!
public class PayeeList extends JInternalFrame {

	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(this.getClass());
	private PayeeForm payeeform;

	final JPopupMenu popupmenu = new JPopupMenu();

	private JTable jTable0;
	private JScrollPane jScrollPane0;

	private ApplicationFrame getApplicationFrame() {
		Container myparent = null;
		myparent = this.getParent();
		while (myparent.getClass().getName() != ApplicationFrame.class
				.getName()) {
			myparent = myparent.getParent();
		}
		return (ApplicationFrame) myparent;
	}

	public PayeeList() {
		initComponents();
	}

	private void initComponents() {
		setClosable(true);
		setMaximizable(true);
		setResizable(true);
		setVisible(true);
		setLayout(new GroupLayout());
		add(getJScrollPane0(), new Constraints(new Bilateral(0, 0, 22),
				new Bilateral(0, 0, 26)));
		setSize(520, 318);
		setTitle("Payee List");
		initPopupMennu0();
	}

	private void initPopupMennu0() {
		// Create and add a menu item
		JMenuItem item1, item2, item3, item4, item5;
		item1 = new JMenuItem("Add...");
		item2 = new JMenuItem("Edit");
		item3 = new JMenuItem("Delete");
		item4 = new JMenuItem("Search");
		item5 = new JMenuItem("Block");

		item1.addActionListener(new MenuItemActionListener());
		item2.addActionListener(new MenuItemActionListener());
		item3.addActionListener(new MenuItemActionListener());
		item4.addActionListener(new MenuItemActionListener());
		item5.addActionListener(new MenuItemActionListener());

		popupmenu.add(item1);
		popupmenu.add(item2);
		popupmenu.add(item3);
		popupmenu.add(item4);
		popupmenu.add(item5);
		popupmenu.addPopupMenuListener(new PopupPrintListener());

	}

	class MenuItemActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			logger.debug(e.getActionCommand());
			if (e.getActionCommand().equals("Add...")) {
				ShowNewPayeeForm();
			} 
			if (e.getActionCommand().equals("Edit")) {
				if(jTable0.getSelectedRowCount()>0) {
				long payeeid = Long.parseLong( jTable0.getModel().getValueAt(
						jTable0.getSelectedRow(), 0).toString() );
//				MessageBox.info(payee);
				ShowEditPayeeForm(payeeid);
			}
//				ShowNewPayeeForm();
			} 
		}
	}

	// Inner class to print information in response to popup events
	class PopupPrintListener implements PopupMenuListener {

		@Override
		public void popupMenuCanceled(PopupMenuEvent e) {
			logger.debug("z");
		}

		@Override
		public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
			logger.debug("z");
		}

		@Override
		public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
			logger.debug("z");
		}
	}

	private void ShowNewPayeeForm() {
		logger.debug("Running ");
		payeeform = getPayeeform();
		payeeform.getBtnAction().setText("Add");
		payeeform.setVisible(true);
		payeeform.toFront();
	}
	
	private void ShowEditPayeeForm(Long payeeid) {
		payeeform = getPayeeform();
		payeeform.getBtnAction().setText("Save");
		payeeform.PrepareEditPayee(payeeid);
		payeeform.setVisible(true);
		payeeform.toFront();		
	}
	
	private JScrollPane getJScrollPane0() {
		if (jScrollPane0 == null) {
			jScrollPane0 = new JScrollPane();
			jScrollPane0.setComponentPopupMenu(popupmenu);
			jScrollPane0.setViewportView(getJTable0());
		}
		return jScrollPane0;
	}

	private AbstractTableModel getPayeeListTable() {
		try {
			List<Payee> payeelist = new Payee().getList("1=1");
			PayeeListTable payeelisttable = new PayeeListTable(payeelist);
			return payeelisttable;
		} catch (EDMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	private JPopupMenu getPopupmenu() {
		popupmenu.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent event) {
				logger.debug("X");
			}
		});
		return popupmenu;
	}

	private JTable getJTable0() {

		if (jTable0 == null) {
			jTable0 = new JTable();
			jTable0.setModel(getPayeeListTable());
			// jTable0.setModel(new DefaultTableModel(new Object[][] { { "0x0",
			// "0x1", }, { "1x0", "1x1", }, }, new String[] { "Title 0",
			// "Title 1", }) {
			// private static final long serialVersionUID = 1L;
			// Class<?>[] types = new Class<?>[] { Object.class, Object.class,
			// };
			//	
			// public Class<?> getColumnClass(int columnIndex) {
			// return types[columnIndex];
			// }
			// });

			jTable0.addMouseListener(new MouseAdapter() {
				public void mousePressed(MouseEvent evt) {
					if (evt.isPopupTrigger()) {
						logger.debug(evt.getComponent().toString());
						popupmenu.show(evt.getComponent(), evt.getX(), evt
								.getY());
					}
				}

				public void mouseReleased(MouseEvent evt) {
					if (evt.isPopupTrigger()) {
						logger.debug(evt.getComponent().toString());
						popupmenu.show(evt.getComponent(), evt.getX(), evt
								.getY());
					}
				}
				// @Override
				// public void mouseReleased(MouseEvent event) {
				// jScrollPane0MouseMouseClicked(event);
				// }
			});

		}
		return jTable0;
	}

	public PayeeForm getPayeeform() {
		if(payeeform == null){
			logger.debug("Payeeform = null");
			payeeform = new PayeeForm();
			payeeform.addInternalFrameListener(new InternalFrameAdapter(){
				@Override
				public void internalFrameClosed(InternalFrameEvent event) {
					logger.debug(event.toString());
					// Refresh payee list.
					jTable0.setModel(getPayeeListTable());
				}
			});
			payeeform.clearForm();
		}
//		logger.debug(payeeform.isValid());
//		logger.debug(payeeform.isVisible());
		if(!payeeform.isValid()){
			this.getApplicationFrame().addChildFrame(payeeform);
		}
		return payeeform;
	}

}
